package com.ttctracker.xmlparser;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.ttctracker.classes.StopItem;

public class MapStopPredictionHandler extends DefaultHandler{
	private StopItem currentStopItem;
	private HashMap<String, String> stopDetails;
	private boolean gotDirection=false;
	private HashMap<String, Object> resultWrapper;
	//private ArrayList<StopItem> stopItems;
	
	public HashMap<String, Object> getResultWrapper() {
		return resultWrapper;
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("predictions")) {
			currentStopItem = new StopItem();
			stopDetails = new HashMap<String, String>();
		}
	}
	/**
	 * 
	 */
	public MapStopPredictionHandler() {
		currentStopItem = new StopItem();
		stopDetails = new HashMap<String, String>();
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		super.endPrefixMapping(prefix);
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (localName.equals("predictions")) {
			stopDetails.put("routeTag", attributes.getValue("routeTag"));
			stopDetails.put("routeTitle", attributes.getValue("routeTitle"));
			currentStopItem.set_tag(attributes.getValue("stopTag"));
			currentStopItem.set_title(attributes.getValue("stopTitle"));
			String noPredictionString = attributes
					.getValue("dirTitleBecauseNoPredictions");
			if (noPredictionString != null) {
				currentStopItem.get_prediction().add(-1);
			}
		} else if (localName.equals("direction")&&!gotDirection) {
			stopDetails.put("directionTitle", attributes.getValue("title"));
			gotDirection=true;
		} else if (localName.equals("prediction")) {
			currentStopItem.get_prediction().add(
					Integer.parseInt(attributes.getValue("minutes")));
		}
		return;
	}
}
